package com.ruoyi.web.controller.utils;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * Created by lzm on 2021/07/30.
 * Json封装查询结果
 */
@Data
@NoArgsConstructor
@ApiModel(description = "通用参数")
public class JR<T> implements Serializable {


    @ApiModelProperty(value="响应业务状态", required=true, position=1)
    private Integer status;


    @ApiModelProperty(value="响应消息", required=true, position=2)
    private String msg;


    @ApiModelProperty(value="响应中的数据", required=true, position=3)
    private T data;

    /**
     * 盈华需要
     */
    @ApiModelProperty(value="响应业务code", required=true, position=1)
    private Integer code;

    public static JR build(Integer status, String msg, Object data,Integer code) {
        return new JR(status, msg, data,code);
    }


    public static JR ok(Object data) {
        return new JR(data);
    }

    public static JR ok() {
        return new JR(null);
    }



    public static JR build(Integer status, String msg,Integer code) {
        return new JR(status, msg, null,code);
    }


    public JR(Integer status, String msg, T data,Integer code) {
        this.status = status;
        this.msg = msg;
        this.data = data;
        this.code = code;
    }





    public JR(T data) {
        this.status = 200;
        this.msg = "OK";
        this.data = data;
        this.code = 200;
    }

    @JSONField(serialize=false)
    @JsonIgnore
    @ApiModelProperty(hidden = true)
    public boolean isSuccess()
    {
        return this.status != null && this.status.intValue() == 200;
    }

    @JSONField(serialize=false)
    @JsonIgnore
    @ApiModelProperty(hidden = true)
    public T  getDataSuc(){
        if(isSuccess()){
            return this.data;
        }
        return null;
    }

    @Override
    public String toString() {
        return "JR{" +
                "status=" + status +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

}
